<?php
class PersonnelContracttypeController  extends Zone_Action{
	public function fields($default=NULL){
		$data = array(
			title 	=> array(
				type 		=> 'CHAR',
				form_label  => 'Tiêu đề',			
				no_empty	=> true
			),
			created_by_id =>array(
				type			=> 'NONE',
				default_value	=> get_user_id()
			)
		);
		return $data;	
	}
	
	public function initAction(){
		$posts = $this->Model->fetchAll("
			SELECT `a`.*,`b`.`username`,`b`.`ID` as `user_id`
			FROM `departments` as `a`
			LEFT JOIN `users` as `b` ON `a`.`created_by_id`=`b`.`ID`
                WHERE `a`.`parent_id`<>0
                  AND `a`.`parent_id` is not null ORDER BY `ID`
		");

		$this->set(array(
			setting				=> array(
				basic_salary	=> 850000	
			),
			side_departments	=> $posts,
            salary_params       => $this->Model->fetchAll("SELECT * FROM `personnels_salary_params`")
		));
	}
	
	public function indexAction(){			
		$posts = $this->Model->fetchAll("SELECT `a`.*,`b`.`username`,`b`.`ID` as `user_id` 
			FROM `personnels_contract_types` as `a` 
			LEFT JOIN `users` as `b` 
				ON `a`.`created_by_id`=`b`.`ID` ORDER BY `title`");
		
		$this->set('posts',$posts);	
	}
	
	public function addAction(){
		if(isPost()){
			loadClass('ZData');
			$form = new ZData();
			$form->addfields( $this->fields());
			$data = $form->getData();
			if( !is_array($data) ){
				return $this->setJSON(array(
					message => error($data)
				));
			}	
			
			$this->Model->insert('personnels_contract_types',$data);
			
			return $this->setJSON(array(
				redirect => '#Personnel/Contracttype'
			));
		}
	}
	
	public function editAction(){
		
		$post_id = getInt('ID',0);		
		$post = $this->Model->fetchRow("SELECT * FROM `personnels_contract_types` WHERE `ID`='$post_id'");
		
		if( !$post ){
			return $this->setJSON(array(
				content => error('Bản ghi đã bị xóa hoặc không tồn tại')
			));		
		}
		
		$this->set('post',$post);		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addfields( $this->fields($post));
			$data = $form->getData();
		
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));
			}		
						
			$this->Model->update('personnels_contract_types',$data,"`ID`='$post_id'");
			return $this->setJSON(array(
				redirect	=> '#Personnel/Contracttype'
			));
		}
	}	

	public function deleteAction(){
		$post_id = getInt("ID",0);
	
		$this->Model->delete('personnels_contract_types'," `ID`='$post_id' ");		
		return $this->setJSON(array(
			redirect	=> '#Personnel/Contracttype'
		));
	}
}
?>